﻿using System;

namespace SecondClassAssignment{
    
    public delegate void EventHandler<BigMoneyArgs>(object sender,BigMoneyArgs e);
    
    public class ATM{

        public event EventHandler<BigMoneyArgs> BigMoneyFetched;
        protected virtual void OnBigMoneyFetched(BigMoneyArgs e){
            if (e.Balance >= 1000 && BigMoneyFetched != null)
                BigMoneyFetched.Invoke(this,e);
        }
        private bool CashDetect(double count){
            var rand = new Random();
            for (var i = 0; i < (int)(count / 100); ++i) {
                if (rand.Next(10) < 3)
                    return false;
            }
            return true;
        }
        
        //取钱
        public bool Withdraw(double count,Account account){
            if (count > account.Balance || count <= 0)
                return false;
            //执行大金额警告函数
            OnBigMoneyFetched(new BigMoneyArgs(account.Balance));
            account.Balance -= count;
            
            return true;
        }
        //存钱
        public bool Save(double count,Account account){
            if (count <= 0)
                return false;
            
            if (!CashDetect(count))
                throw new BadCashException("存入的钞票有坏钞!");
            
            account.Balance += count;
            return true;
        }
    }
}